//
//  XMWithdrawView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/16.
//

#import "XMWithdrawView.h"

@interface XMWithdrawView ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *withdrawNumTextField;
@property (nonatomic, strong) UILabel *bankNameLabel;
@property (nonatomic, strong) UIButton *stateBtn;
@property (nonatomic, strong) UILabel *canDrawNumLabel;

@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, copy) NSString *balanceStr;

@end

@implementation XMWithdrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"交易提现";
    self.backgroundColor = kColorHEX(0xF7F7F7);
    
    UIView *explainBgView = [[UIView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, KScaleWidth(48.0))];
    [explainBgView setPartCornerWithRectCorner:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:KScaleWidth(14.0)];
    explainBgView.backgroundColor = kColorHEX(0xFDF7E7);
    [self addSubview:explainBgView];
    {
        UIImageView *titleIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), 0, KScaleWidth(14.0), explainBgView.height)];
        titleIcon.contentMode = UIViewContentModeScaleAspectFit;
        titleIcon.image = ImageNamed(@"mine_withdraw_title_icon");
        [explainBgView addSubview:titleIcon];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(30.0), 0, explainBgView.width - KScaleWidth(30.0), explainBgView.height) font:KFontSize(14.0) textColor:kColorHEX(0xE5AD54)];
        titleLabel.text = @"买家购买的产品，订单完成后钱款将会到达余额";
        [explainBgView addSubview:titleLabel];
    }
    UIView *contentBgView = [[UIView alloc] initWithFrame:CGRectMake(0, explainBgView.maxY, self.width, KScaleWidth(210.0))];
    contentBgView.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:contentBgView];
    
    UIImageView *weiIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), KScaleWidth(10.0), KScaleWidth(34.0), KScaleWidth(40.0))];
    weiIcon.contentMode = UIViewContentModeScaleAspectFit;
    weiIcon.image = ImageNamed(@"mine_wei_withdraw_icon");
    [contentBgView addSubview:weiIcon];
    
    [contentBgView addSubview:self.bankNameLabel];
//    [contentBgView addSubview:self.stateBtn];
//    _bankNameLabel.hidden = YES;
    
    UILabel *withdrawTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), KScaleWidth(63.0), self.width, KScaleWidth(31.0)) font:KBoldFontSize(15.0) textColor:kColorHEX(0x333333)];
    withdrawTitleLabel.text = @"提现金额";
    [contentBgView addSubview:withdrawTitleLabel];
    
    UILabel *moneyTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(KScaleWidth(14.0)), withdrawTitleLabel.maxY + KScaleWidth(10.0), KScaleWidth(30.0), KScaleWidth(41.0)) font:KBoldFontSize(28.0) textColor:kColorHEX(0x333333)];
    moneyTitleLabel.text = @"¥";
    [contentBgView addSubview:moneyTitleLabel];
    [contentBgView addSubview:self.withdrawNumTextField];
    _withdrawNumTextField.y = moneyTitleLabel.y;
    
    UIButton *withdrawBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(self.width - KScaleWidth(85.0), moneyTitleLabel.y, KScaleWidth(85.0), moneyTitleLabel.height) title:@"全部提现" titleColor:kColorHEX(0xEB3535)];
    [withdrawBtn addTarget:self action:@selector(withdrawAllAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentBgView addSubview:withdrawBtn];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), withdrawBtn.maxY + KScaleWidth(7.0), self.width - KScaleWidth(15.0)*2, 1)];
    lineView.backgroundColor = kColorHEX(0xE4E4E4);
    [contentBgView addSubview:lineView];
    [contentBgView addSubview:self.canDrawNumLabel];
    
    UIButton *checkDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(16.0) frame:CGRectMake(0, contentBgView.maxY + KScaleWidth(10.0), self.width, KScaleWidth(54.0)) title:@"提现明细" titleColor:kColorHEX(0x333333)];
    checkDetailBtn.backgroundColor = kColorHEX(0xffffff);
    checkDetailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [checkDetailBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, KScaleWidth(14.0), 0, 0)];
    [checkDetailBtn addTarget:self action:@selector(checkDetailAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:checkDetailBtn];
    
    UIImageView *nextIcon = [[UIImageView alloc] initWithFrame:CGRectMake(checkDetailBtn.width - KScaleWidth(14.0) - KScaleWidth(8.0), 0, KScaleWidth(8.0), checkDetailBtn.height)];
    nextIcon.contentMode = UIViewContentModeScaleAspectFit;
    nextIcon.image = ImageNamed(@"next_icon_black");
    [checkDetailBtn addSubview:nextIcon];
    
    UIButton *withdrawSubBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, checkDetailBtn.maxY + KScaleWidth(68.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"提现" titleColor:kColorHEX(0x333333)];
    [withdrawSubBtn setBackgroundImage:[UIImage gradientColorImageFromColors:@[kColorHEX(0xFFD800), kColorHEX(0xFFE44E)] gradientType:GradientTypeLeftToRight imgSize:withdrawSubBtn.size] forState:UIControlStateNormal];
    [withdrawSubBtn setCornerRadius:withdrawSubBtn.height/2];
    [withdrawSubBtn addTarget:self action:@selector(withdrawAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:withdrawSubBtn];
    
    UIButton *withdrawExplainBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake((self.width - KScaleWidth(120.0))/2, withdrawSubBtn.maxY + KScaleWidth(6.0), KScaleWidth(120.0), KScaleWidth(41.0)) title:@"提现说明" titleColor:kColorHEX(0x6B6B6B) image:@"ming_withdraw_exp_icon"];
    [withdrawExplainBtn addTarget:self action:@selector(withdrawExplainAction:) forControlEvents:UIControlEventTouchUpInside];
    [withdrawExplainBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(4.0)];
    [self addSubview:withdrawExplainBtn];
    
    [self addSubview:self.selectBtn];
    _selectBtn.y = withdrawExplainBtn.maxY + KScaleWidth(53.0);
    
    //服务条款
    UILabel *serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(50.0), _selectBtn.y, self.width - KScaleWidth(50.0), _selectBtn.height)];
    NSMutableAttributedString *serviceAttr = [[NSMutableAttributedString alloc] initWithString:@"登录表示您已同意本平台《服务条款》和《隐私政策》" attributes:@{NSFontAttributeName : KFontSize(13.0)}];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(11, 5)];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(18, 5)];
    [serviceLabel setAttributedText:serviceAttr];
    [self addSubview:serviceLabel];
    
    CGFloat width = [@"登录表示您已同意本平台" getWidthWithFontSize:KFontSize(13.0)];
    
    UIButton *serviceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    serviceBtn.frame = CGRectMake(serviceLabel.x + width, serviceLabel.y, KScaleWidth(90.0), serviceLabel.height);
    [serviceBtn addTarget:self action:@selector(serviceCheckAction:) forControlEvents:UIControlEventTouchUpInside];
//    serviceBtn.backgroundColor = kColorHEX(0xff00ff);
    [self addSubview:serviceBtn];
    UIButton *privacyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    privacyBtn.frame = CGRectMake(serviceBtn.maxX + KScaleWidth(15.0), serviceBtn.y, serviceBtn.width, serviceBtn.height);
//    privacyBtn.backgroundColor = kColorHEX(0xff00ff);
    [privacyBtn addTarget:self action:@selector(privacyCheckAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:privacyBtn];
}

- (void)setUserBalance:(NSString *)balanceStr {
    if (![_balanceStr isKindOfClass:[NSString class]]) {
        _balanceStr = [NSString stringWithFormat:@"%@", balanceStr];
    }
    else {
        _balanceStr = balanceStr;
    }
    _canDrawNumLabel.text = [NSString stringWithFormat:@"可提现金额%@元", balanceStr];
}

//绑定银行卡
- (void)bandBankAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(bandBankCardAction)]) {
        [self.delegate bandBankCardAction];
    }
}

//全部提现
- (void)withdrawAllAction:(UIButton *)button {
    _withdrawNumTextField.text = _balanceStr;
}

//提现
- (void)withdrawAction:(UIButton *)button {
    if (_withdrawNumTextField.text.length == 0 || [_withdrawNumTextField.text floatValue] <= 0) {
        [MBProgressHUD showError:@"请输入提现金额"];
        return;
    }
    if (!_selectBtn) {
        XMWeakSelf;
        UIAlertController *alertVC = [UIAlertController initwithTitle:@"" message:@"" titles:@[@"确定", @"取消"] itemSelect:^(NSInteger selectIndex) {
            
        }];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(withdrawActionWithPayType:payAccmount:money:)]) {
        [self.delegate withdrawActionWithPayType:@"1" payAccmount:@"13522446643" money:_withdrawNumTextField.text];
    }
}

//提现明细
- (void)checkDetailAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkWithdrawDetailAction)]) {
        [self.delegate checkWithdrawDetailAction];
    }
}

//提现说明
- (void)withdrawExplainAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkWithdrawExplainAction)]) {
        [self.delegate checkWithdrawExplainAction];
    }
}

//服务条款
- (void)serviceCheckAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkServiceAction)]) {
        [self.delegate checkServiceAction];
    }
}

//隐私政策
- (void)privacyCheckAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkPrivacyAction)]) {
        [self.delegate checkPrivacyAction];
    }
}

- (void)selectAction:(UIButton *)button {
    button.selected = !button.selected;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length > 0) {
        if ([string isEqual:@"."]) {
            if (textField.text.length == 0) {
                textField.text = @"0";
                return YES;
            }
            else if ([textField.text containsString:@"."]) {
                return NO;
            }
        }
        else if (textField.text.length > 0) {
            if ([textField.text containsString:@"."]) {
                if ([textField.text componentsSeparatedByString:@"."][1].length >= 2) {
                    return NO;
                }
            }
        }
    }
//    NSLog(@"text = %@", textField.text);
//    NSLog(@"%@", string);
    return YES;
}

- (void)editChanged:(UITextField *)textField {
    if (textField.text.length > 0) {
        if ([textField.text floatValue] > [_balanceStr floatValue]) {
            textField.text = _balanceStr;
            [MBProgressHUD showError:@"超出余额"];
        }
    }
}

#pragma mark - Init
- (UILabel *)bankNameLabel {
    if (!_bankNameLabel) {
        _bankNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(60.0), KScaleWidth(15.0), self.width - KScaleWidth(60.0) - KScaleWidth(70.0), KScaleWidth(31.0)) font:KFontSize(15.0) textColor:kColorHEX(0x1E1C1C)];
        _bankNameLabel.text = @"微信零钱";
    }
    return _bankNameLabel;
}

- (UIButton *)stateBtn {
    if (!_stateBtn) {
        _stateBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(self.width - KScaleWidth(77.0), KScaleWidth(15.0), KScaleWidth(77.0), KScaleWidth(31.0)) title:@"未绑定" titleColor:kColorHEX(0x666666) image:@"mine_detail_icon"];
        [_stateBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(8.0)];
        [_stateBtn addTarget:self action:@selector(bandBankAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _stateBtn;
}

- (UITextField *)withdrawNumTextField {
    if (!_withdrawNumTextField) {
        _withdrawNumTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(47.0), 0, self.width - KScaleWidth(47.0) - KScaleWidth(100.0), KScaleWidth(41.0))];
        _withdrawNumTextField.font = KFontSize(15.0);
        _withdrawNumTextField.placeholder = @"请输入提现金额";
        _withdrawNumTextField.keyboardType = UIKeyboardTypeDecimalPad;
        _withdrawNumTextField.delegate = self;
        [_withdrawNumTextField addTarget:self action:@selector(editChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _withdrawNumTextField;
}

- (UILabel *)canDrawNumLabel {
    if (!_canDrawNumLabel) {
        _canDrawNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(15.0), self.withdrawNumTextField.maxY + KScaleWidth(18.0), self.width - KScaleWidth(15.0), KScaleWidth(31.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
        _canDrawNumLabel.text = @"可提现金额1000元";
    }
    return _canDrawNumLabel;
}

- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(23.0), 0, KScaleWidth(26.0), KScaleWidth(26.0)) image:@"login_service_select_icon" selectImg:@"login_service_select_icon_selected"];
        [_selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
